package com.vidyu.businesslayer.businesscomponents;

import com.google.common.collect.Sets;
import com.vidyu.businesslayer.businessentities.Contractor;
import com.vidyu.businesslayer.businessentities.Employer;
import com.vidyu.businesslayer.businessentities.Skill;
import com.vidyu.datalayer.dataaccesscomponents.ContractorDao;
import com.vidyu.datalayer.dataaccesscomponents.EmployerDao;
import com.vidyu.datalayer.dataaccesscomponents.SkillDao;
import com.vidyu.servicelayer.servicedataentitties.ContractorRO;
import com.vidyu.servicelayer.servicedataentitties.EmployerRO;
import com.vidyu.servicelayer.servicedataentitties.SkillsRO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: eyalsa
 * Date: 4/6/13
 * Time: 3:30 PM
 * To change this template use File | Settings | File Templates.
 */
@Component
public class ContractorHandler {

    @Autowired
    private ContractorDao contractorDao;

    @Autowired
    private SkillDao skillDao;

    public long addContractor(ContractorRO contractorRO) {
        Contractor contractor = contractorDao.getContractorByEmail(contractorRO.getEmail());
        long id = Constants.NON_VALID_ID;
        if (contractor == null) {
            id = contractorDao.save(new Contractor(contractorRO.getName(), contractorRO.getEmail(), contractorRO.getPassword(),
                    contractorRO.getAddress(), contractorRO.getCountry(), contractorRO.pricePerHour, contractorRO.title,
                    contractorRO.contraDesc));
        }
        return id;
    }

    public Contractor getContractorByEmailAndPassword(String email, String password) {
        return contractorDao.getContractorByEmailAndPassword(email, password);
    }

    public Contractor findByID(long id) {
        return contractorDao.findById(id);
    }

    public void setSkillsToContractor(SkillsRO skillsRO) {
        Contractor contractor = contractorDao.findById(skillsRO.contractorId);
        Set<Skill> skillSet;
        if (contractor.getSkillSet() == null) {
            skillSet = Sets.newHashSet();
        } else {
            skillSet = contractor.getSkillSet();
            contractor.setSkillSet(null);
            contractorDao.update(contractor);

            for (Skill skill : skillSet) {
                skillDao.delete(skill);
            }
            skillSet.clear();
        }
        Skill skill;
        if (skillsRO.skill1 != null && skillsRO.skillDesc1 != null) {
            skill = new Skill(skillsRO.skill1, skillsRO.skillDesc1);
            skillDao.save(skill);
            skillSet.add(skill);
        }
        if (skillsRO.skill2 != null && skillsRO.skillDesc2 != null) {
            skill = new Skill(skillsRO.skill2, skillsRO.skillDesc2);
            skillDao.save(skill);
            skillSet.add(skill);
        }

        if (skillsRO.skill3 != null && skillsRO.skillDesc3 != null) {
            skill = new Skill(skillsRO.skill3, skillsRO.skillDesc3);
            skillDao.save(skill);
            skillSet.add(skill);
        }
        if (skillsRO.skill4 != null && skillsRO.skillDesc4 != null) {

            skill = new Skill(skillsRO.skill4, skillsRO.skillDesc4);
            skillDao.save(skill);
            skillSet.add(skill);
        }
        if (skillsRO.skill5 != null && skillsRO.skillDesc5 != null) {
            skill = new Skill(skillsRO.skill5, skillsRO.skillDesc5);
            skillDao.save(skill);
            skillSet.add(skill);
        }

        contractor.setSkillSet(skillSet);

        contractor.setFirstProt(skillsRO.firstProt);
        contractor.setSecondProt(skillsRO.secondProt);
        contractor.setThirdProt(skillsRO.thirdProt);
        contractor.setForthProt(skillsRO.forthProt);
        contractor.setFifthProt(skillsRO.fifthProt);

        contractorDao.update(contractor);
    }
}
